package xcmg.device.web.company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.CompanyDealerLimitService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.company.CompanyDealerVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/dealerLimit")
public class CompanyDealerLimitController extends BasicController{

    @Autowired
    private CompanyDealerLimitService companyDealerLimitService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<CompanyDealerVO> condition) throws Exception
    {
        CompanyDealerVO companyDealerVO=condition.getFilters();
        if(companyDealerVO==null){
            companyDealerVO=new CompanyDealerVO();
        }
        if(companyDealerVO.getOrgId()==null){
            companyDealerVO.setOrgId(getOrgId());
        }
        condition.setFilters(companyDealerVO);
        PageCommonVO pageCommonVO=companyDealerLimitService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/updateLimit",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult updateLimit(@RequestBody List<CompanyDealerVO> dealerVOList){
        try {
            return actionResult(companyDealerLimitService.updateLimit(dealerVOList));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/importLimitExcel", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult importLimitExcel(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result=new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            result= companyDealerLimitService.importLimit(file, getOrgId());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/downloadLimitExcel",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void downloadLocationExcel() throws Exception {
        companyDealerLimitService.downloadLimitExcel();
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(CompanyDealerVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        companyDealerLimitService.exportAll(condition);
    }

    @RequestMapping(value = "/updateEnableLimit",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult updateEnableLimit(@RequestBody CompanyDealerVO condition)throws Exception{
        return actionResult(companyDealerLimitService.updateEnableLimit(Arrays.asList(condition.getIdArray()), condition.getEnableLimit()));
    }
}
